package util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*创建一个日期的工具类*/
public class DateUtil
{
	/*日期加时间的格式化*/
	static String dateTimeParrern = "yyyy-MM-dd HH:mm:ss";
	/*日期的格式化*/
	private String  dateParrenrn = "yyyy-MM-dd";
	/*将日期时间转化成字符串*/
	public String timeToString(Date now)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(dateTimeParrern);
		return sdf.format(now);
	}
	/*将日期转换成字符串*/
	public  String dateToString(Date now)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(dateParrenrn);
		return sdf.format(now);
	}
	/*
	 * 将日期转换为有格式的字符串
	 */
	public String dateToString(Date now,String pattern)
	{
		/* 参数:有指定格式的日期 */
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(now) ; 
	}
	/*将字符串转变成日期
	 * */
	public static Date StringDate(String now)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(dateTimeParrern);
		try
		{
			sdf.parse(now);
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		
		return new Date();
	}
	/*测试字符串的拼装效果*/
	public static void main(String[] args)
	{
		DateUtil  du = new DateUtil();
//		String sre = du.timeToString(new Date());
//		System.out.println("=="+sre);
		//yyyy-MM-dd HH:mm:ss
		String sr= du.dateToString(new Date(), "dd/MM/yy HH:mm:ss");
		System.out.println("=="+sr);
		String now = du.timeToString(new Date());
		System.out.println("=="+StringDate(now).toLocaleString());
	}
}
